﻿using System;
using System.Collections.Generic;
using System.IO;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Geographical;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms.Users
{
    public partial class MyProfile : AdminUserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);                
            RolePermisionInfo _roleInfo =
                   RoleController.GetReadWritePermison(CurrentUser.UserID, CurrentCommand.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {
                btnUpdate.Visible = false;
            }
        }
        public void LoadCityInfo()
        {
            /*
            List<CityInfo> _lstCities = GeographicalController.GetCities(1);
            drCity.DataTextField = "Name";
            drCity.DataValueField = "CityID";
            drCity.DataSource = _lstCities;
            drCity.DataBind();
             */
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            lblUpdateStatus.Text = string.Empty;
            if (!IsPostBack)
            {
                txtAddress.Attributes.Add("onkeyup", "ismaxlength(this)");
                if (CurrentUser != null)
                {
                    txtFirstName.Text = CurrentUser.FirstName;
                    txtLastName.Text = CurrentUser.LastName;
                    txtAddress.Text = CurrentUser.Address;
                    txtCmtnd.Text = CurrentUser.Cmtnd;
                    if (CurrentUser.Avatar == string.Empty)
                    {
                        imgAvartar.ImageUrl = "~/Images/NO_PHOTO_AVAILABLE.jpg";
                    }
                    else
                    {
                        imgAvartar.ImageUrl = MultimediaUtilities.GetAvatar(CurrentUser.Avatar);
                    }
                    txtMobile.Text = CurrentUser.Mobile;
                    txtTel.Text = CurrentUser.Tel;
                    txtEmail.Text = CurrentUser.Email;
                  
                    txtAdditionalInfo.Text = CurrentUser.AdditionalInfo;
                    txtYm.Text = CurrentUser.Ym;
                    txtSkype.Text = CurrentUser.Skype;
                    txtUsername.Text = CurrentUser.Username;
                    if (CurrentUser.Gender)
                    {
                        rdoGender.SelectedValue = "0";
                    }
                    else
                        rdoGender.SelectedValue = "1";
                    LoadCityInfo();
                    drCity.SelectedValue = CurrentUser.City.ToString();
                    radBirth.SelectedDate = CurrentUser.Birth;
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentUser.Username = txtUsername.Text.Trim();
                CurrentUser.FirstName = txtFirstName.Text.Trim();
                CurrentUser.LastName = txtLastName.Text.Trim();
                CurrentUser.Mobile = txtMobile.Text.Trim();
                CurrentUser.Tel = txtTel.Text.Trim();
                CurrentUser.Address = txtAddress.Text.Trim();
                CurrentUser.Cmtnd = txtCmtnd.Text.Trim();
                CurrentUser.Email = txtEmail.Text.Trim();
                CurrentUser.AdditionalInfo = txtAdditionalInfo.Text.Trim();
                CurrentUser.Ym = txtYm.Text.Trim();
                CurrentUser.Skype = txtSkype.Text.Trim();
          
                MultimediaUtilities.SetAvatarThumbnail(Server.MapPath(CurrentUser.Avatar), 120, 0);
                CurrentUser.Birth = radBirth.SelectedDate;
                if (rdoGender.SelectedValue == "0")
                {
                    CurrentUser.Gender = true;
                }
                else
                {
                    CurrentUser.Gender = false;
                }
                CurrentUser.City = ConvertUtility.ToInt32(drCity.SelectedValue);
                UserController.UpdateUser(CurrentUser);
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID, CurrentUser.UserID
                                            , CurrentUser.FirstName + " " + CurrentUser.LastName, 1, DateTime.Now);
                if (CurrentUser.Avatar != null)
                {
                    imgAvartar.ImageUrl = CurrentUser.Avatar;
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.DEFAULT_CP_PAGE);
        }
    }
}